package com.superbytecode.stencil.entities;

import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * All rights Reserved, Designed By www.super-master.com
 *
 * @ProjectName: code-stencil
 * @Package: com.superbytecode.stencil.entity
 * @ClassName: Pagination
 * @Description: [自定义分类工具实体]
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2024/10/15 9:53
 * @Version: V1.0
 * @Copyright: 2024 www.super-master.cn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Data
public class Pagination<T> implements Serializable {
    private static final long serialVersionUID = -4663557719539484887L;
    private Integer total;
    /**
     * 总页数
     */
    private Integer pages;
    /**
     * 当前页
     */
    private Integer pageNum;
    /**
     * 每页条数，默认10条
     */
    private Integer pageSize = 10;
    /**
     * 分页总数据条数
     */
    private List<T> data;

    /**
     * 分页包装器
     *
     * @param data     数据条数
     * @param total    总数
     * @param pageNum  当前页
     * @param pageSize 每页条数
     * @param <T>      泛型
     * @return 分页包装对象
     */
    public static <T> Pagination<T> wrap(List<T> data, int total, int pageNum, int pageSize) {
        Pagination<T> pagination = new Pagination<>();
        pagination.setData(data);
        pagination.setTotal(total);
        pagination.setPageNum(pageNum);
        if (pageSize <= 0) {
            pageSize = 10;
        }
        pagination.setPageSize(pageSize);
        int pages = (int) Math.ceil((double) total / pageSize);
        pagination.setPages(pages);
        return pagination;
    }
}